﻿/*============================================
 * 类名 :ScanPlug
 * 描述 :
 *   
 * 创建时间: 2011-1-26 21:58:28
 * Blog:   http://home.cnblogs.com/xiangism
 *============================================*/
using System;
using System.Collections.Generic;

using System.Text;
using System.IO;
using MathAssistLibrary;
using System.Reflection;
using System.Windows.Forms;

namespace MathAssist
{
    /// <summary>扫描插件</summary>
    class ScanPlug
    {
        ToolStripMenuItem tsmiPlug;

        public ScanPlug(ToolStripMenuItem tsmi) {
            this.tsmiPlug = tsmi;
        }

        public List<ICommand> Refurbish(string fold) {

            if (!Directory.Exists(fold)) {
                throw new DirectoryNotFoundException("没有找到插件目录，找新建plug目录");
            }
            List<String> files = FindDllFile(fold);
            List<ICommand> cmds = new List<ICommand>();

            foreach (string i in files) {
                List<object> obj = LoadOne(i, typeof(MathAssistLibrary.ICommand));

                foreach (object o in obj) {
                    if (o is ICommand) {
                        cmds.Add(o as ICommand);
                    }
                }
                List<object> frm = LoadOne(i, typeof(IForm));

                foreach (object o in frm) {
                    if (o is IForm) {
                        ToolStripMenuItem tsmi = new ToolStripMenuItem();
                        tsmi.Text = (o as IForm).Text;
                        tsmi.Tag = (o as IForm).GetForm;
                        tsmi.Click += new EventHandler(MeunItemClick);
                        tsmiPlug.DropDownItems.Add(tsmi);
                    }
                }

            }
            return cmds;
        }


        void MeunItemClick(object sender, EventArgs e) {
            IForm obj = (sender as ToolStripMenuItem).Tag as IForm;
            if (obj != null) {
                obj.GetForm.Show();
            }

        }

        List<string> FindDllFile(string foldername) {
            DirectoryInfo dir = new DirectoryInfo(foldername);
            FileInfo[] files = dir.GetFiles();
            List<string> result = new List<string>();

            foreach (FileInfo fi in files) {
                if (fi.Name.ToUpper().EndsWith(".DLL"))
                    result.Add(fi.FullName);
            }
            return result;
        }

        private List<object> LoadOne(string filename, Type type) {
            List<object> result = new List<object>();

            try {
                Assembly ass = Assembly.LoadFrom(filename);
                Module[] mods = ass.GetModules();

                foreach (Module mod in mods) {
                    Type[] typs = mod.GetTypes();

                    foreach (Type typ in typs) {
                        if (type.IsAssignableFrom(typ)) {
                            result.Add(ass.CreateInstance(typ.FullName));
                        }
                    }
                }
            } catch (BadImageFormatException) {

            }
            return result;
        } // end func

    }

}
